#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>s
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=4e5+1000;
typedef int LL;
inline LL read(){LL x=0,f=1;char ch=getchar();	while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;}
LL tree[maxn][26],endp[maxn],pt=0;
char s[maxn];
void ins(const char* s){
     LL cur=0;LL n=strlen(s);
     for(int i=0;i<n;i++){
         LL c=s[i]-'a';
         if(!tree[cur][c]) tree[cur][c]=++pt;
         endp[tree[cur][c]]++;
         cur=tree[cur][c];
     }
}
LL query(const char* s){
    LL cur=0;LL n=strlen(s);
    for(int i=0;i<n;i++){
        LL c=s[i]-'a';
        if(!tree[cur][c]) return 0;
        cur=tree[cur][c];
    }
    return endp[cur];
}
int main(void){

    while(gets(s)){
        if(strlen(s)==0) break;
        ins(s);
    }
    while(gets(s)){
        printf("%d\n",query(s));
    }
   	return 0;
}
